package com.liu.resume.viewmodel;

import com.alibaba.fastjson.JSON;
import com.liu.resume.MyApplication;
import com.liu.resume.model.UserModel;
import com.liu.resume.util.OfflineJsonMgr;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import rx.Observer;

/**
 * Created by mgs
 */
public class UserOperateViewModel {

    LoginObserver observer;

    public UserOperateViewModel(LoginObserver listener) {
        this.observer = listener;
    }

    /*
     * 登录
     * */
    public void login(String username, String pwd) {
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(pwd);
        userModel.login(new SaveListener<UserModel>() {
            @Override
            public void done(UserModel userModel, BmobException e) {
                if (e == null) {
                    getUserModel();
                } else {
                    observer.onError(e);
                }
            }
        });
    }

    /*
     *注册
     * */
    public void register(final String username, final String pwd) {
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(pwd);
        userModel.setName(username);//初始昵称为用户名
        userModel.signUp(new SaveListener<UserModel>() {
            @Override
            public void done(UserModel userModel, BmobException e) {
                if (e == null) {
                    //sdk的Bug，注册后不能修改信息，只能注册后再登录
                    login(username, pwd);
                } else {
                    if (observer == null) return;
                    if (e.getErrorCode() == 202) {
                        observer.onNext("该账号已被注册，请换个账号");
                    } else {
                        observer.onError(e);
                    }
                }
            }
        });
    }

    /*
     * 更新用户资料
     * */
    public void update(UserModel userModel) {
        UserModel model = MyApplication.INSTANCE.getUserInfo();
        if (model == null) {
            //第三方登录时，model为空
            model = BmobUser.getCurrentUser(UserModel.class);
        }
        userModel.update(model.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    getUserModel();
                } else {
                    if (observer == null) return;
                    if (e.getErrorCode() == 206) {
                        //由于注册后并没有登录的bug
                        observer.onNext("账号失效，请重新登录");
                    } else {
                        observer.onError(e);
                    }

                }
            }
        });
    }

    /*
     * 更改密码
     * */
    public void updatePwd(String oldPwd, String newPwd) {
        UserModel.updateCurrentUserPassword(oldPwd, newPwd, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    if (observer == null) return;
                    observer.onCompleted();
                } else {
                    if (observer == null) return;
                    if (e.getErrorCode() == 210) {
                        observer.onNext("修改失败，旧密码错误");
                    } else {
                        observer.onError(e);
                    }
                }
            }
        });
    }
    public interface LoginObserver<String> extends Observer<String>{
         void onNext(String string);
    }



    //-----------------内部调用-------------------------


    /*
     *获取网络用户资料给UserModel,并缓存
     * */
    private void getUserModel() {
        if (observer == null) return;
        //获取数据
        UserModel user = BmobUser.getCurrentUser(UserModel.class);
        MyApplication.INSTANCE.setUserInfo(user);
        //加入缓存
        OfflineJsonMgr.getInstance(MyApplication.INSTANCE).saveJson(JSON.toJSONString(user), UserModel.class.getSimpleName());
        observer.onCompleted();
    }


}
